/**
 * 生成随机数
 * @param len 长度
 * @param date 是否加上时间戳
 */
export const randomStrByLenAndDate = (len: number, date: boolean = false): string => {
    let random: string = '';
    random = Math.ceil(Math.random() * 100000000000000)
        .toString()
        .substring(0, len || 4);
    if (date) random = random + Date.now();
    return random;
};


/**
 * 字符串脱敏
 * @param content 要加密的字符
 * @param preNum 前面多少个字符不脱敏
 * @param subNum 后面多少个字符不脱敏
 * @param flag 不填flag，preNum与subNum为空时，默认只显示后4位，其他为**
 *               设为true，则不处理
 * @param keepLength *的长度保持与字符一样
 */
export const desensitization = (content: string, preNum: string | undefined = undefined,
                                subNum: string | undefined = undefined, flag: boolean | undefined = false,
                                keepLength: number | undefined = undefined): string => {
    var subNum1: string | undefined = subNum;
    if (!preNum && !subNum && !flag) {
        subNum1 = "4";
    }
    var maxStarLen: number = keepLength || 4;
    if (content === null || content === undefined || content.toString().trim().length <= 0) {
        return '';
    } else {
        let c: string = content.toString().trim();
        let result: string = '';
        let len: number = c.length;
        if (preNum) {
            let preNumInt: number = parseInt(preNum);
            if (preNumInt >= len) {
                return content;
            } else {
                result += c.substring(0, preNumInt);
                len = len - preNumInt;
            }
        }
        if (subNum1) {
            let subNumInt: number = parseInt(subNum1);
            if (subNumInt >= len) {
                return content;
            } else {
                let s = len - subNumInt;
                if (s > maxStarLen) {
                    s = maxStarLen;
                }
                while (s > 0) {
                    result += '*';
                    s--;
                }
                result += c.substring(c.length - subNumInt, c.length);
            }
        }
        return result;
    }
};

/**
 * 判断传入的东西是否是json格式的数据
 * @param str1
 * @returns {boolean}
 */
export const isJSON = (str1: any): boolean => {
    let str = str1;

    if (!str1) {
        return false;
    }
    if (str instanceof FormData) {
        return false;
    }

    if (typeof str == 'string') {
        try {
            let obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
                return true;
            } else {
                return false;
            }
        } catch (e) {
            return false;
        }
    }

    if (str && typeof str == 'object') {
        try {
            let t1: string = JSON.stringify(str);
            if (!t1 || t1 === '{}' || t1.indexOf('{') === -1 || t1.indexOf('}') === -1) {
                return false;
            }
            let t2 = JSON.parse(t1);
            let t3: string = JSON.stringify(t2);
            return t1 === t3;
        } catch (e) {
            return false;
        }
    }

    return false;
};


//去除对象中'',null,undefined属性
export const removeEmptyField = (obj: any) => {
    var newObj: any = {}
    if (typeof obj === 'string') {
        obj = JSON.parse(obj)
    }
    if (obj instanceof Array) {
        newObj = []
    }
    if (obj instanceof Object) {
        for (var attr in obj) {
            // 属性值不为'',null,undefined才加入新对象里面(去掉'',null,undefined)
            if (obj.hasOwnProperty(attr) && obj[attr] !== '' && obj[attr] !== null && obj[attr] !== undefined) {
                if (obj[attr] instanceof Object) {
                    // 空数组或空对象不加入新对象(去掉[],{})
                    if (JSON.stringify(obj[attr]) === '{}' || JSON.stringify(obj[attr]) === '[]') {
                        continue
                    }
                    // 属性值为对象,则递归执行去除方法
                    newObj[attr] = removeEmptyField(obj[attr])
                } else if (
                    typeof obj[attr] === 'string' &&
                    ((obj[attr].indexOf('{') > -1 && obj[attr].indexOf('}') > -1) ||
                        (obj[attr].indexOf('[') > -1 && obj[attr].indexOf(']') > -1))
                ) {
                    // 属性值为JSON时
                    try {
                        var attrObj = JSON.parse(obj[attr])
                        if (attrObj instanceof Object) {
                            newObj[attr] = removeEmptyField(attrObj)
                        }
                    } catch (e) {
                        newObj[attr] = obj[attr]
                    }
                } else {
                    newObj[attr] = obj[attr]
                }
            }
        }
    }
    return newObj
}
